package com.szc.demo01.controller.boss;

import com.szc.demo01.controller.base.BaseController;
import com.szc.demo01.exception.NotBossException;
import com.szc.demo01.pojo.Dept;
import com.szc.demo01.pojo.Emp;
import com.szc.demo01.pojo.User;
import com.szc.demo01.pojo.dto.UserDto;
import com.szc.demo01.service.DeptService;
import com.szc.demo01.service.EmpService;
import com.szc.demo01.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 老板修改员工信息
 * @Author: ShiZhiCheng
 * @Date: 2021/01/14 16:17:48
 */
@Controller
public class BossEmpController extends BaseController {

    @Resource
    UserService userService;

    @Resource
    EmpService empService;

    @Resource
    DeptService deptService;

    /**
     * 展示所有员工信息
     */
    @GetMapping("/boss_showUser.html")
    public String bossShowUser(Model model) throws NotBossException {
        //权限校验
        assertUserIdHasPerm(1);

        List<UserDto> userDtoList = userService.getAllUserDto();
        model.addAttribute("userDtoList",userDtoList);
        return "boss/boss_showUser_table";
    }

    /**
     * 来到修改员工信息页面
     */
    @GetMapping("/toBoss_edit")
    public String toBossEdit(@RequestParam("id") long id, Model model) throws NotBossException {
        assertUserIdHasPerm(1);
        //查出所有员工信息
        User user = userService.getById(id);
        Emp emp = empService.getById(id);
        Dept dept = deptService.getById(emp.getDeptno());
        UserDto userDto = new UserDto(user, emp, dept);
        model.addAttribute("userDto",userDto);

        //查出所有部门
        List<Dept> deptList = deptService.list();
        model.addAttribute("deptList",deptList);

        return "boss/boss_editable_table";
    }

    /**
     * 保存修改
     */
    @PostMapping("/boss_edit")
    public String bossEdit(UserDto userDto) throws NotBossException {
        assertUserIdHasPerm(1);
        User user = new User(userDto);
        Emp emp = new Emp(userDto);
        //保存信息
        userService.updateById(user);
        empService.updateById(emp);

        return "redirect:/boss_showUser.html";
    }

    /**
     * 注册员工
     * @return
     */
    @PostMapping("/boss_regist")
    public String regist(UserDto userDto) throws NotBossException {
        assertUserIdHasPerm(1);
        User user = new User(userDto);
        userService.save(user);

        Emp emp = new Emp(userDto);
        emp.setId(user.getId());
        emp.setHiredate(new Date());
        empService.save(emp);

        return "redirect:/boss_showUser.html";
    }

    @GetMapping("/boss_delete")
    public String regist(@RequestParam("id") long id) throws NotBossException {
        assertUserIdHasPerm(1);
        userService.removeById(id);
        empService.removeById(id);
        return "redirect:/boss_showUser.html";
    }
}
